package Objects;

/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.concurrent.Callable;

import Tools.Utiles;
import actions.PlatarBomba.ContadorBombas;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.util.GameTaskQueueManager;
import com.jmex.physics.PhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.StaticPhysicsNode;

public class Bomba extends Node implements Runnable {

	// Display a message, preceded by the name of the current thread
	static void threadMessage(String message) {
		String threadName = Thread.currentThread().getName();
		System.out.format("%s: %s%n", threadName, message);
	}
	private int alto = 20;
	private int ancho = 20;

	public ContadorBombas contador;
	private float countdown = 3;

	private boolean[][] mazeData;

	private Vector3f pos;

	private int potencia = 10;
	private Node scene;

	private float tileSize = 10;
	private PhysicsSpace physicsSpace;
	private StaticPhysicsNode bomba;

	public Bomba(Vector3f pos, PhysicsSpace physicsSpace,
			boolean[][] mazeData2, Node rootNode, ContadorBombas contador) {
		super("bomba");
		this.pos = pos;
		this.mazeData = mazeData2;
		this.scene = rootNode;
		this.physicsSpace = physicsSpace;

		Box model = new Box("player", new Vector3f(), 2, 2, 2);

		bomba = physicsSpace.createStaticNode();
		locateBomb();
		bomba.attachChild(model);
		this.contador = contador;
		this.contador.instalarBomba();
		this.attachChild(bomba);

	}

	private void detonar() {

		int[] a = Utiles.calcularPosicionEnTablero(pos);

		for (int dist = 1; dist <= potencia; dist++) {
			// TOP
			if (a[1] < alto - 1 - dist && mazeData[a[0]][(a[1] + dist)] == true) {
				mazeData[a[0]][(a[1] + dist)] = false;
				String name = "mazeBox_(" + a[0] + ", " + (a[1] + dist) + ")";
				final Spatial c = scene.getChild(name);
				final PhysicsNode cc = (PhysicsNode) c;

				GameTaskQueueManager.getManager().update(
						new Callable<Object>() {
							public Object call() throws Exception {
								cc.delete();

								return null;

							}
						});

			}

			// LEFT
			if (a[1] > dist && mazeData[a[0]][(a[1] - dist)] == true) {
				mazeData[a[0]][(a[1] - dist)] = false;
				String name = "mazeBox_(" + a[0] + ", " + (a[1] - dist) + ")";
				final Spatial c = scene.getChild(name);

				this.getParent().detachChild(c);
				final PhysicsNode cc = (PhysicsNode) c;

				GameTaskQueueManager.getManager().update(
						new Callable<Object>() {
							public Object call() throws Exception {
								cc.delete();

								return null;
							}
						});

			}

			// RIGHT
			if (a[0] < ancho - 1 - dist
					&& mazeData[(a[0] + dist)][a[1]] == true) {
				mazeData[(a[0] + dist)][a[1]] = false;
				String name = "mazeBox_(" + (a[0] + dist) + ", " + (a[1]) + ")";

				final Spatial c = scene.getChild(name);
				final PhysicsNode cc = (PhysicsNode) c;

				GameTaskQueueManager.getManager().update(
						new Callable<Object>() {
							public Object call() throws Exception {
								cc.delete();
							
								return null;
							}
						});

			}

			// DOWN
			if (a[0] > dist && mazeData[a[0] - dist][a[1]] == true) {
				mazeData[a[0] - dist][a[1]] = false;
				String name = "mazeBox_(" + (a[0] - dist) + ", " + (a[1]) + ")";

				final Spatial c = scene.getChild(name);
				final PhysicsNode cc = (PhysicsNode) c;

				GameTaskQueueManager.getManager().update(
						new Callable<Object>() {
							public Object call() throws Exception {
								cc.delete();
								return null;
							}
						});

			}
		}

		GameTaskQueueManager.getManager().update(
				new Callable<Object>() {
					public Object call() throws Exception {
						bomba.delete();
						return null;
					}
				});
	

	}

	private void locateBomb() {
		pos.x = (int) (pos.x / tileSize);
		pos.z = (int) (pos.z / tileSize);

		pos.x = pos.x * tileSize + tileSize / 2;
		pos.y = pos.y + 10;
		pos.z = pos.z * tileSize + tileSize / 2;
		this.setLocalTranslation(pos.x, 10, pos.z);

	}

	public void run() {
		while (countdown > 0) {
			try {
				Thread.sleep(1000);
				countdown--;
				threadMessage("s" + countdown);

			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		detonar();
		contador.detonarBomba();
		
	}

}
